/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("ExamineList",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using Examine.IServices;
using Examine.IRepositories;
using System.Linq;
using Microsoft.AspNetCore.Authorization;

namespace Examine.Controllers
{
    [Route("api/ExamineComparison")]
    public partial class ExamineListController
    {
        private readonly IExamineListService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IExamineListRepository _repository;
        private readonly IComparisonDataRepository _comparisonDataRepository;

        [ActivatorUtilitiesConstructor]
        public ExamineListController(
            IExamineListService service,
            IHttpContextAccessor httpContextAccessor,
            IExamineListRepository repository,
            IComparisonDataRepository comparisonDataRepository
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            _repository = repository;
            _comparisonDataRepository = comparisonDataRepository;
        }

        [HttpPost, Route("pushData"), AllowAnonymous]
        public async Task<IActionResult> pushData([FromBody] ComparisonData param)
        {
            bool rst = false;

            // 创建主表数据
            ExamineList mainData = new();

            if (param.Type == 1)
            {
                mainData.Number = param.Code;
                mainData.Status = 0;

                _repository.Add(mainData);
                _repository.SaveChanges();

                // 明细绑定主表Id
                param.ExamineListId = mainData.ExamineListId;

                _comparisonDataRepository.Add(param);
                int result = _comparisonDataRepository.SaveChanges();

                if (result > 0)
                {
                    mainData.HarvestId = param.Id;
                    rst = true;
                }
            }
            else
            {
                // 查询主表数据
                var listQuery = _repository.FindAsIQueryable(x => true);
                mainData = listQuery.Where(x => x.Number == param.Code).First();

                if (mainData != null && mainData.ExamineListId != 0)
                {
                    param.ExamineListId = mainData.ExamineListId;

                    _comparisonDataRepository.Add(param);
                    int result = _comparisonDataRepository.SaveChanges();

                    if (result > 0)
                    {
                        // 更新主表数据
                        mainData.IntegrationId = param.Id;
                        mainData.Status = 1;
                        mainData.Time = new System.DateTime();
                        rst = true;
                    }
                }
            }

            if (rst)
            {
                _repository.Update(mainData);
                rst = _repository.SaveChanges() > 0;
            }

            return Json(rst);
        }

        [HttpPost, Route("detail")]
        public async Task<IActionResult> detail([FromBody] ExamineList param)
        {
            var query = _comparisonDataRepository.FindAsIQueryable(x => true);
            var list = query.Where(x => x.ExamineListId == param.ExamineListId).ToList();

            return Json(list);
        }

        [HttpPost, Route("check")]
        public async Task<IActionResult> check([FromBody] ApiComparsionInput param)
        {
            bool rst = false;

            param.Comparsion.CreateDate = new System.DateTime();
            _comparisonDataRepository.Add(param.Comparsion);
            int result = _comparisonDataRepository.SaveChanges();

            if (result > 0)
            {
                _comparisonDataRepository.Update(param.Harvest);
                _comparisonDataRepository.Update(param.Integration);
                _comparisonDataRepository.SaveChanges();

                param.MainData.CheckId = param.Comparsion.Id;
                _repository.Update(param.MainData);
                rst = _repository.SaveChanges() > 0;
            }

            return Json(rst);
        }
    }
}
